package com.zhangtai.modules.service.impl.common;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.NoticeUserRoleDao;
import com.zhangtai.modules.entity.common.NoticeUserRoleEntity;
import com.zhangtai.modules.service.common.NoticeUserRoleService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class NoticeUserRoleServiceImpl extends ServiceImpl<NoticeUserRoleDao, NoticeUserRoleEntity> implements NoticeUserRoleService {

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<NoticeUserRoleEntity> page = this.page(
                new Page<NoticeUserRoleEntity>(pageNum,pageSize),
                new QueryWrapper<NoticeUserRoleEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    /**
     * 获取当前角色下所有人员
     * @param roleId
     * @return
     */
    public List<NoticeUserRoleEntity> getAllRoleUser(@RequestParam(value = "roleId", required = false) Long roleId){
        return this.list(new QueryWrapper<NoticeUserRoleEntity>().eq("notice_role_id",roleId));
    }

    @Override
    public NoticeUserRoleEntity getByIdE(Long id) {
            NoticeUserRoleEntity one = this.getOne(new QueryWrapper<NoticeUserRoleEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(NoticeUserRoleEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(NoticeUserRoleEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            NoticeUserRoleEntity entity = new NoticeUserRoleEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}